<?php

require_once 'KgridList.php';

//$this->_KgridListing =  new KgridList();
$KgridListing =  new KgridList();




$strAction = $_REQUEST["action"];           //get action from another web page

$strPK_ID = $_REQUEST["num"]; 

$intRowIDGet = $_REQUEST["rowid"]; 
$intPageIDGet = $_REQUEST["pageid"];  
$intSkipGet = $_REQUEST["skip"];  

//get Sorted by field and PageSize, that are passed as well
$strSortField = $_REQUEST["sortby"];  
$strPageSizeGet = $_REQUEST["pagesize"];  

  if ($strPageSizeGet == ""
            || strlen($strPageSizeGet) == 0)
 {
                  $intPageSizeGet = 1;     //so as not to divide by 0
 }
 else
 {
                  $intPageSizeGet = $strPageSizeGet;
 }


 //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
 //Please change fields to be updated
 //------------------------------------------
                //$strmachtosave = $_REQUEST["machtosave"];   
                //$strarraytosave = $_REQUEST["arraytosave"];  
                //$strstartdatetosave = $_REQUEST["startdatetosave"];  
                //$strenddatetosave = $_REQUEST["enddatetosave"];  
                //$strnotetosave = $_REQUEST["notetosave"];   
                //$blnchktosave = $_REQUEST["chktosave"];  



if (isset($_REQUEST["arraysearch"]))
{
   $strarraysearch = $_REQUEST["arraysearch"];
                
}
	else
{
   $strarraysearch = "";
}





//END Fields to be Updated
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
//Next Sort and Search fields are for if using LIST in conjunction (Do not need for Standalone Update form)
//----------------------------------------------------------------------------------------------------------
                //$strarraysearch = $_REQUEST["arraysearch"];  
                //$strmachsearch = $_REQUEST["machsearch"]; 
                $strmachsearch = "";
             
//END Optional Fields               
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&


              GetSkipCount($strSortField, $strPageSizeGet, $strPK_ID, $intSkipGet, $intRowIDGet);

              UpdateDBase($strPK_ID);


               $strReinitialise = "";


             
                      $intRowIDGet = $intRowIDGetGlobal;
                      $intPageIDGet = $intPageIDGetGlobal;
                      $intSkipGet = $intSkipGetGlobal;
                
                     //echo $runSkipCount;
                     //echo $strPageSizeGet;


                     $ActionPage = "GetCurrentPage";

                     $KgridListing->ajaxkgridlist($ActionPage, $strarraysearch, $strmachsearch, $intPageIDGet, $strSortField, $intSkipGet, $intRowIDGet, $strPK_IDNEW, $strReinitialise);





    function GetSkipCount($strSortField, $strPageSizeGet, $strPK_ID, $intSkipGet, $intRowIDGet)
    {
    //--------------------------------------------------------------------------------------------------------------------
    // PURPOSE SUMMARY:- Re-calculate PageID, RowID and SkipCount (if search or sort fields changed)
    //                                       So as to display in List correctly.
    //
    // PSEUDO CODE:-  Select PK_ID from Array table using unique Sortedby key
    //                              Read all records until PK_ID of record on page matches
    //                               when match found exit out read immediately
    //                               Re-calculate PageID, RowID and SkipCount, using derived RecordCount & PageSize
    //---------------------------------------------------------------------------------------------------------------------

        global $intRowIDGetGlobal;
        $intRowIDGetGlobal = 0;
        global $intPageIDGetGlobal;
        $intPageIDGetGlobal = 0;
        global $intSkipGetGlobal;
        $intSkipGetGlobal = 0;

        global $runSkipCount;
        global $strPK_IDNEW;


        $intRecordCount = $intSkipGet + $intRowIDGet;

        if ($intRecordCount == 0)
        {
          $intRowIDGetGlobal = 0;
          $intPageIDGetGlobal = 1;
          $intSkipGetGlobal = 0;
        }
        else
        {

         $intresult = $intRecordCount / $strPageSizeGet;
         $intresult = Floor($intresult);
         $intremainder = $intRecordCount % $strPageSizeGet;


         if ($intremainder == 0) 
         {
          $intRowIDGetGlobal = ($strPageSizeGet - 1);                                 //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if ($intRowIDGetGlobal < 0)                                //check tha rowid is not < 0
          {
            $intRowIDGetGlobal = 0;
          }
          $intPageIDGetGlobal = $intresult;
          $intSkipGetGlobal = ($intresult * $strPageSizeGet) - $strPageSizeGet;
         }

         if ($intremainder > 0) 
         {
          $intRowIDGetGlobal = ($intremainder - 1);                                      //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if ($intRowIDGetGlobal < 0)                              //check tha rowid is not < 0
          {
            $intRowIDGetGlobal = 0;
          }
          $intPageIDGetGlobal = $intresult + 1;
          $intSkipGetGlobal = (($intresult + 1) * $strPageSizeGet) - $strPageSizeGet;
         }

        }  //if ($intRecordCount == 0)


        $runSkipCount = "True";

         //echo $intCounter;

        //return $intCounter;

    }




    function GetSkipCountOLD($strSortField, $strPageSizeGet, $strPK_ID)
    {
    //--------------------------------------------------------------------------------------------------------------------
    // PURPOSE SUMMARY:- Re-calculate PageID, RowID and SkipCount (if search or sort fields changed)
    //                                       So as to display in List correctly.
    //
    // PSEUDO CODE:-  Select PK_ID from Array table using unique Sortedby key
    //                              Read all records until PK_ID of record on page matches
    //                               when match found exit out read immediately
    //                               Re-calculate PageID, RowID and SkipCount, using derived RecordCount & PageSize
    //---------------------------------------------------------------------------------------------------------------------

        global $intRowIDGetGlobal;
        $intRowIDGetGlobal = 0;
        global $intPageIDGetGlobal;
        $intPageIDGetGlobal = 0;
        global $intSkipGetGlobal;
        $intSkipGetGlobal = 0;

        global $runSkipCount;
        global $strPK_IDNEW;
        $intwherecounter = 0;
        $intCounter = 0;
        $strAtFirstRecord = "No";
        //SqlCommand objCmd;
        $strSQL = "";
         
        $dsn2="SQLSERVER2005";
        $username2="sa";
        $password2="and";
  
        $sqlconnect2=odbc_connect($dsn2,$username2,$password2);

         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
         //Change as required
         $strSQL = "SELECT PK_ID FROM ARRAYS2 ";                             //customise to your Table
         $strSQL = $strSQL . " ORDER BY " . $strSortField;                    //customise to your Table
         //$strSQL = $strSQL . " ORDER BY PK_ID";                             //customise to your Table
         //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

        //echo $strSQL;

        $rs=odbc_exec($sqlconnect2, $strSQL);


        //if (!$rs)
        // {
        //    echo "error in Connecting to do Count";
        // }

         $intRecordCount = 0;
         $strnotfound = "Yes";

         while (odbc_fetch_row($rs))
         {

           if ($strAtFirstRecord == "Yes")
           {
              $strPK_IDNEW = odbc_result($rs,"PK_ID");
              $intRowIDGetGlobal = 0;
              $intPageIDGetGlobal = 1;
              $intSkipGetGlobal = 0;
              break;
           }

           $intRecordCount = $intRecordCount + 1;

           if (odbc_result($rs,"PK_ID") == $strPK_ID)
           {

             if ($intRecordCount == 1)            //at first record
             {
              $strAtFirstRecord = "Yes";
             }
           
             if ($intRecordCount > 1)            //if Not at first record
             {
              $intRecordCount = $intRecordCount - 1;
              $strPK_IDNEW = $strPrevPKID;  
             }

              break;
           }
 
           $strPrevPKID = odbc_result($rs,"PK_ID");        //Change to suit Primary Key

         }



        odbc_close($sqlconnect2);

       

        $intresult = $intRecordCount / $strPageSizeGet;
        $intresult = Floor($intresult);
        $intremainder = $intRecordCount % $strPageSizeGet;

        

        if ($intremainder == 0) 
        {
          $intRowIDGetGlobal = ($strPageSizeGet - 1);                                 //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if ($intRowIDGetGlobal < 0)                                //check tha rowid is not < 0
          {
            $intRowIDGetGlobal = 0;
          }
          $intPageIDGetGlobal = $intresult;
          $intSkipGetGlobal = ($intresult * $strPageSizeGet) - $strPageSizeGet;
        }

        if ($intremainder > 0) 
        {
          $intRowIDGetGlobal = ($intremainder - 1);                                      //must always take 1 off pageid (ie because row 1 is now row 0, for AJAX grid list)
          if ($intRowIDGetGlobal < 0)                              //check tha rowid is not < 0
          {
            $intRowIDGetGlobal = 0;
          }
          $intPageIDGetGlobal = $intresult + 1;
          $intSkipGetGlobal = (($intresult + 1) * $strPageSizeGet) - $strPageSizeGet;
        }


        $runSkipCount = "True";

         //echo $intCounter;

        //return $intCounter;

    }





    function UpdateDBase($strPK_ID)
    {

        $dsn2="SQLSERVER2005";
        $username2="sa";
        $password2="and";
  
        $odbc = odbc_connect($dsn2,$username2,$password2);

        //$odbc = odbc_connect ('InterestDB', '', '') or die('Could Not Connect to ODBC Database!');

        //$update = "UPDATE ARRAYS2 SET Array_Name=$bal_new, Interest=$c_interest, Last_Update='$modified' WHERE ID=$userID";

        
        $update = "DELETE FROM ARRAYS2 WHERE PK_ID = " . $strPK_ID . "";

        //echo $update;
		
        odbc_exec($odbc,$update);

        odbc_close($odbc);


   }







?>
